<template>
  <body class="bg2">
  <el-form class="login-container" label-position="left"
           label-width="0px" v-loading="loading">
    <h3>用户注册</h3>
    <p style="color: red;">{{nameHint}}</p>
    <el-form-item>
      <el-input type="text" v-model="loginForm.username"
                auto-complete="off" placeholder="用户名"
      @blur="checkName"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input type="password" v-model="loginForm.password"
                auto-complete="off" placeholder="密码" show-password></el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%;border: none" v-on:click="register">注册</el-button>
    </el-form-item>

    <el-form-item>
      <el-link type="warning" style="float:left" href="/login">已有账户？去登录</el-link>
      <el-link type="primary" style="float:right" href="/">返回</el-link>
    </el-form-item>
  </el-form>
  </body>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,   /*v-loading   动态添加数据的时候，请求没有成功之前会会出现一个【正在加载标示】；请求成功，显示数据【正在加载的标示】消失。*/
      nameHint:'',
    }
  },
  methods: {
    checkName(){
      var _this = this
      var url = '/register/check?username='+this.loginForm.username
      this.$axios.get(url)
          .then(resp => {
             // alert(resp.data.code)
            if (resp.data.code === 200) {
              _this.nameHint=''
            } else {
              _this.nameHint=resp.data.message
            }
          })
          .catch(failResponse => {
          })
    },
    register() {
      var _this = this
      this.$axios
          .post('/register', {
            username: this.loginForm.username,
            password: this.loginForm.password
          })
          .then(resp => {
            // alert(resp.data.code)
            if (resp.data.code === 200) {
              this.$alert('注册成功', '成功提示', {
                confirmButtonText: '确定'
              })
              _this.$router.replace('/login')
            } else {
              this.$alert(resp.data.message, '错误提示', {
                confirmButtonText: '确定'
              })
            }
          })
          .catch(failResponse => {
          })
    }
  }
}
</script>
<style>
*{
  z-index:0;
}
[v-cloak] {
  display: none !important;
}
.bg2{
  top: 0;
  left: 0;
  margin: 0px;
  padding: 0;
  background: url("../assets/bg1.jpg") no-repeat;
  background-size: cover;
  height: 100%;
  width: 100%;
  position: fixed;
  z-index: -1;
}

.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
</style>

